package org.ielse.frame;

import java.util.Map;

import org.ielse.frame.BaseActivity.VolleyListener;
import org.ielse.frame.util.BaseUtil;
import org.ielse.frame.util.LogUtil;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

public class BaseFragment extends Fragment {

	protected BaseActivity activity;

	@Override public void onAttach(Activity activity) {
		super.onAttach(activity);
		LogUtil.d(getClass().getSimpleName() + " onAttach()");
		if (activity instanceof BaseActivity) {
			this.activity = (BaseActivity) activity;
		}
	}

	@Override public void onDetach() {
		super.onDetach();
		LogUtil.d(getClass().getSimpleName() + " onDetach()");
		this.activity = null;
	}

	@Override public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LogUtil.d(getClass().getSimpleName() + " onCreate()");
	}

	@Override public void onDestroy() {
		LogUtil.d(getClass().getSimpleName() + " onDestroy()");
		super.onDestroy();
	}

	public void showToast(int resId) {
		if (activity != null) activity.showToast(resId);
	}

	public void showToast(String text) {
		if (activity != null) activity.showToast(text);
	}

	public void startActivity(Class<?> target, Object... params) {
		if (activity != null) activity.startActivity(target, BaseUtil.initBundle(params));
	}

	public void startActivity(Class<?> target, Bundle bundle) {
		if (activity != null) activity.startActivity(target, bundle);
	}

	public <T> void post(final String url, final Map<String, String> params, final Class<T> clazz, final VolleyListener<T> listener) {
		if (activity != null) activity.post(url, params, clazz, listener);
	}

	public <T> void get(final String url, final Map<String, String> params, final Class<T> clazz, final VolleyListener<T> listener) {
		if (activity != null) activity.get(url, params, clazz, listener);
	}

	public View inflater(int resource) {
		return activity != null ? activity.inflater(resource) : null;
	}

	public void showDialog(View content, boolean canCancel, boolean shadow) {

	}
}
